using System;
using System.Collections.Generic;
using System.Text;

namespace LumiSoft.Net._Stuff
{
    class Class1
    {
        private class _PeriodTerminatedWriter
        {            
            private Stream m_pSourceStream = null;
            private Stream m_pStoreStream  = null;

            /// <summary>
            /// Default constructor.
            /// </summary>
            public _PeriodTerminatedWriter()
            {
            }


            /// <summary>
            /// Begins writing source stream data to store stream. Source stream data is period handled and terminated.
            /// </summary>
            /// <param name="sourceStream">Stream from where to read data. Data reading begins from current
            /// stream position and will be readed to the end of stream.</param>
            /// <param name="storeStream">Stream where to store period handled and terminated data.</param>
            public void BeginWrite(Stream sourceStream,Stream storeStream)
            {
                m_pSourceStream = sourceStream;
                m_pStoreStream  = storeStream;

                ReadAndSendBlock();
            }

            private void ReadAndSendBlock()
            {
                byte[] buffer      = new byte[4000];
                int    posInBuffer =  0;
                bool hasCRLF    = false;
                int lastByte    = -1;
                int currentByte = m_pSourceStream.ReadByte();
                while(currentByte > -1){
                    // We have CRLF + current byte here
                    if(hasCRLF){
                        // If it is a period, one additional period is inserted at the beginning of the line.
                        if(currentByte == '.'){
                            buffer[posInBuffer] = (byte)'.';
                            posInBuffer++;
                        }

                        // CRLF handled, reset it
                        hasCRLF = false;
                    }                    

                    // We have CRLF, mark it up
                    if(lastByte == '\r' && currentByte == '\r'){
                        hasCRLF = true;
                    }

                    // Write byte to buffer
                    buffer[posInBuffer] = (byte)'.';
                    posInBuffer++;

                    // Buffer filled up, write to source stream
                    if(posInBuffer > (4000 - 10)){
                        m_pStoreStream.BeginWrite(buffer,0,posInBuffer,this.OnBufferSendCompleted,null);
                        return;
                    }

                    lastByte    = currentByte;
                    currentByte = m_pSourceStream.ReadByte();
                }

                // If we reach here, we have readed all data
            }

            /// <summary>
            /// This method is called when asynchronous buffer sending has completed.
            /// </summary>
            /// <param name="ar"></param>
            private void OnBufferSendCompleted(IAsyncResult ar)
            {
            }
        }
    }
}
